package kuangstudy;

/**
 * @author:李雄
 * @create:2022-07-05 16:32
 */
public class Race implements Runnable {
    public int rabbit_speed = 10;
    public int tortoise_speed = 5;
    public int rabbit_dis=0;
    public int tortoise_dis=0;

    @Override
    public void run() {
        for (int i = 0; i <= 100; i++) {
            if(Thread.currentThread().getName().equals("兔子")){
                if(i%10==0){
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else {
                    rabbit_dis=rabbit_speed*i;
                    System.out.println(Thread.currentThread().getName()+"跑了"+rabbit_dis+"米");
                }
            }else {
                tortoise_dis=tortoise_speed*i;
                System.out.println(Thread.currentThread().getName()+"跑了"+tortoise_dis+"米");
            }
            if(rabbit_dis>=500||tortoise_dis>=500){
                if(rabbit_dis>=500){
                    System.out.println("兔子获胜");
                }else {
                    System.out.println("乌龟获胜");
                }
                break;
            }
        }
    }

    public static void main(String[] args) {
        Race race = new Race();
        Thread rabbit = new Thread(race, "乌龟");
        Thread tortoise = new Thread(race, "兔子");
        rabbit.start();
        tortoise.start();
    }
}
